/*
 * Copyright (c) 2008, intarsys consulting GmbH
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published by the 
 * Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 */
package de.intarsys.pdf.example.viewer.awt;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JToolBar;

import de.intarsys.pdf.example.common.CommonJPodRendererExample;
import de.intarsys.pdf.parser.COSLoadException;

/**
 * A very simple PDF viewer application.
 * 
 */
@SuppressWarnings("serial")
public class Viewer extends CommonJPodRendererExample {

	public static void main(String[] args) {
		Viewer client = new Viewer();
		try {
			client.run(args);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private Action firstAction = new AbstractAction("First") {
		public void actionPerformed(ActionEvent e) {
			pdfCanvas.selectFirstPage();
		}
	};

	private JFrame frame;

	private Action lastAction = new AbstractAction("Last") {
		public void actionPerformed(ActionEvent e) {
			pdfCanvas.selectLastPage();
		}
	};

	private Action nextAction = new AbstractAction("Next") {
		public void actionPerformed(ActionEvent e) {
			pdfCanvas.selectNextPage();
		}
	};

	private Action openAction = new AbstractAction("Open") {
		public void actionPerformed(ActionEvent e) {
			open();
		}
	};

	private PDFCanvas pdfCanvas;

	private Action prevAction = new AbstractAction("Prev") {
		public void actionPerformed(ActionEvent e) {
			pdfCanvas.selectPreviousPage();
		}
	};

	private JScrollPane scrollPane;

	private JToolBar toolBar;

	private Action zoomInAction = new AbstractAction("Zoom in") {
		public void actionPerformed(ActionEvent e) {
			pdfCanvas.setScale(pdfCanvas.getScale() * 1.2);
		}
	};

	private Action zoomOutAction = new AbstractAction("Zoom out") {
		public void actionPerformed(ActionEvent e) {
			pdfCanvas.setScale(pdfCanvas.getScale() / 1.2);
		}
	};

	private void createGUI() {
		frame = new JFrame("jPod PDF viewer");
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		toolBar = new JToolBar();
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);
		JButton btOpen = new JButton(openAction);
		toolBar.add(btOpen);
		JButton btFirst = new JButton(firstAction);
		toolBar.add(btFirst);
		JButton btPrev = new JButton(prevAction);
		toolBar.add(btPrev);
		JButton btNext = new JButton(nextAction);
		toolBar.add(btNext);
		JButton btLast = new JButton(lastAction);
		toolBar.add(btLast);
		toolBar.add(new JSeparator());
		JButton btZoomIn = new JButton(zoomInAction);
		toolBar.add(btZoomIn);
		JButton btZoomOut = new JButton(zoomOutAction);
		toolBar.add(btZoomOut);
		toolBar.add(new JSeparator());
		//
		pdfCanvas = new PDFCanvas();
		scrollPane = new JScrollPane(pdfCanvas,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		frame.setSize(600, 800);
		frame.setVisible(true);
	}

	protected void open() {
		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(frame);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			try {
				open(file.getAbsolutePath());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void open(String pathname) throws IOException, COSLoadException {
		super.open(pathname);
		pdfCanvas.setDoc(getDoc());
	}

	public void run(String[] args) throws Exception {
		if (args.length < 0) {
			usage();
			return;
		}
		createGUI();
		if (args.length > 0) {
			String inputFileName = args[0];
			open(inputFileName);
		}
	}

	/**
	 * Help the user.
	 */
	public void usage() {
		System.out.println("usage: java.exe " + getClass().getName() //$NON-NLS-1$
				+ " <input-pdf>"); //$NON-NLS-1$
	}
}
